directives.directive('radioFilter', ["$rootScope", "tools", "$timeout", "$routeParams", "tmp", function ($rootScope, tools, $timeout, $routeParams, tmp) {
    return {
        restrict: 'E',
        scope: true,
        replace: true,
        link: function (scope, element, attr) {
            var config = scope.config = scope.getCondition(attr.conditionid ? Number(attr.conditionid) : Number(attr.tmpid));
            var $ele = $(element);
            var events = {};

            // 搜索
            scope.search = {
                text: ''
            };
            scope.$watch('search.text', function (nv, ov) {
                angular.forEach(scope.datas, function (data) {
                    if (!nv || data.value.indexOf(nv) != -1) {
                        data.shown = true;
                    } else {
                        data.shown = false;
                    }
                });
            });

            scope.clearData = function () {
                scope.datas.length = 0;
                scope.fieldTitle = '';
                $rootScope.$broadcast('FIELD_RECOVER_FROM_COND_EV', null);
            }

            // 获取数据
            scope.datas = [];
            scope.someselected = "";

            scope.stopPropagation = function (e, val) {
                e.stopPropagation();
            }

            scope.selectItem = function (e, val) {
                if (scope.someselected == val) {
                    $rootScope.$broadcast('FILTER_EV', config.conditionId, "");
                    scope.someselected = "";
                    e.stopPropagation();
                    return;
                }

                $rootScope.$broadcast('FILTER_EV', config.conditionId, val.value);
                scope.someselected = val;
                e.stopPropagation();
            }

            scope.clearCondition = function () {
                $rootScope.$broadcast('FILTER_CLEAR_EV', config.conditionId);
            }

            scope.getConditionData = function (conditionId) {
                tools.get({
                    url: '/Ibm-Client/getSelectDatasByConditionId.htm',
                    data: {
                        conditionId: conditionId,
                        pageId: $routeParams.id
                    },
                    succ: function (resp) {
                        if (resp.success) {
                            scope.datas = resp.value;
                            angular.forEach(scope.datas, function (data, index, arr) {
                                arr[index] = {
                                    value: data,
                                    shown: true
                                }
                            });

                            $ele.removeClass(attr.type);

                            $timeout(function () {
                                $ele.find('.data-wrap').height($ele.height() - 65);
                                $ele.find('.data-wrap').mCustomScrollbar({
                                    theme: "minimal-dark"
                                });
                            }, 50);
                        }
                    }
                });
            }

            if (config.conditionId) {
                scope.getConditionData(config.conditionId);
            } else {
                $ele.addClass(attr.type);
            }

            // 监听取消预览事件
            /*events.PREVIEW_OFF_EV_OFF = $rootScope.$on('PREVIEW_OFF_EV', function () {
             scope.selected = "";
             // $ele.find('.data-wrap').scrollTop(0);
             });*/

            events.RESIZE_EV_OFF = $rootScope.$on('RESIZE_EV', function () {
                if (scope.selected) {
                    $ele.find('.data-wrap').height($ele.height() - 65);
                    $ele.find('.data-wrap').mCustomScrollbar({
                        theme: "minimal-dark"
                    });
                }
            });
        },
        templateUrl: "/trend-new-BI/app/components/common/chartManager/radio/radioView.html"
    }
}]);